<!--
 * @Author: dawei 664690250@qq.com
 * @Date: 2023-04-12 10:00:28
 * @LastEditors: dawei 664690250@qq.com
 * @LastEditTime: 2023-04-12 15:22:35
 * @FilePath: /hn-talent-ump-vue/src/pages/system/modules/PlanGroupModal.vue
 * @Description: 请填写文件描述
 * 大佬别走，带带弟弟
 * Copyright (c) 2023 by ${git_name_email}, All Rights Reserved.
-->
<template>
    <a-modal
      :title="title"
      :width="600"
      :visible="visible"
      :confirmLoading="confirmLoading"
      @ok="handleOk"
      @cancel="handleCancel"
      cancelText="关闭"
    >
      <a-spin :spinning="confirmLoading">
        <a-form-model layout="horizontal" labelAlign="right" ref="ruleForm" :label-col="labelCol" :wrapper-col="wrapperCol" :model="form" :rules="validatorRules">
          <a-form-model-item label="用户名称" prop="name">
            <ZSelectMultiUser v-model="form.name"/>
          </a-form-model-item>
        </a-form-model>
      </a-spin>
    </a-modal>
  </template>

<script>
import { addGroupUser } from '@/services/system'
import ZSelectMultiUser from '@/components/zzbiz/ZSelectMultiUser'
export default {
  name: 'PlanGroupModal',
  components: { ZSelectMultiUser },
  props: {
    groupId: {
      type: String
    }
  },
  data() {
    return {
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      form: {},
      validatorRules: {
        name: [{ required: true, message: '请输入用户名称!' }]
      },
      disableSubmit: false
    }
  },
  methods: {
    validateDictCode(rule, value, callback) {
      // 重复校验
      var params = {
        tableName: 'sys_dict',
        fieldName: 'dict_code',
        fieldVal: value,
        dataId: this.model.id
      }
      console.log(params)
      callback()
    },
    add() {
      this.visible = true
    },
    // 确定
    handleOk() {
      // 触发表单验证
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          this.confirmLoading = true
          const formData = Object.assign(this.model, this.form)
          const nameArr = formData.name.split(',')
          const req = nameArr.map(item => {
            return {
              username: item,
              groupId: this.groupId
            }
          })
          addGroupUser(req).then(res => {
            this.resSuccess(res)
          }).finally(() => {
            this.confirmLoading = false
          })
        }
      })
    },
    resSuccess(res) {
      if (res.success) {
        this.$message.success(res.message)
        this.$emit('ok')
        this.close()
      } else {
        this.$message.warning(res.message)
      }
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.$refs.ruleForm.resetFields()
      this.visible = false
    }
  }
}
</script>

